package com.crawler.waf.security.authens;

import org.springframework.security.core.GrantedAuthority;

import java.util.ArrayList;
import java.util.List;
/**
 * 系统通过用户携带的Authorization头信息，验证通过后，封装用户认证信息。包括：<br>
 * 1、用户details
 * 2、用户authority
 * 3、用户token的有效性
 *
 */
public class WafUserAuthentication extends WafAbstractAuthenticationToken {

	private static final long serialVersionUID = 1L;

	private Object token;

	public WafUserAuthentication(
			List<? extends GrantedAuthority> authorities) {
		super(authorities);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GrantedAuthority> getAuthorities() {
		UserCenterUserDetails details = (UserCenterUserDetails) super
				.getDetails();
		if (details!=null)
			return (List<GrantedAuthority>) details.getAuthorities();
		return new ArrayList<>();
	}

	@Override
	public Object getCredentials() {
		return token;
	}

	// 返回用户主体信息,主体信息中包含用户信息 userinfo 和角色信息
	@Override
	public Object getPrincipal() {
		UserCenterUserDetails details = (UserCenterUserDetails) super.getDetails();
		if (details!=null) {
			return details.getUserInfo();
		}
		return null;
	}

	public void setToken(Object token) {
		this.token = token;
	}
	
	public Object getToken(){
		return token;
	}


}
